﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;

namespace BussinesLogic
{
    public abstract class BaseObject
    {
        protected string xmlObjectDescription()
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", String.Empty);
            xmlDoc.PrependChild(xmlDec);
            XmlElement elemRoot = xmlDoc.CreateElement(this.GetType().Name);
            xmlDoc.AppendChild(elemRoot);
            XmlElement elem = null;
            Type idType = this.GetType();
            foreach (PropertyInfo pInfo in idType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                object o = pInfo.GetValue(this, null);
                elem = xmlDoc.CreateElement(pInfo.Name);
                if (o == null)
                {
                    o = System.DBNull.Value;
                }

                if (pInfo.PropertyType == typeof(DateTime))
                {
                    elem.InnerText = String.Format("{0:yyyy/MM/dd}", ((DateTime)o));
                }
                else
                {
                    elem.InnerText = o.ToString();
                }
                elemRoot.AppendChild(elem);
            }
            StringWriter sw = new StringWriter();
            XmlTextWriter tx = new XmlTextWriter(sw);
            xmlDoc.WriteTo(tx);

            return sw.ToString();
        }
    }
}
